package com.pmg;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.input.touch.controller.ITouchController.ITouchEventCallback;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.sensor.accelerometer.AccelerometerData;
import org.anddev.andengine.sensor.accelerometer.IAccelerometerListener;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.graphics.Color;
import android.widget.Toast;

public class Home extends BaseGameActivity implements IAccelerometerListener,
		ITouchEventCallback {

	private static final int CAMERA_WIDTH = 320;
	private static final int CAMERA_HEIGHT = 480;
	private static final int TEXTURE_WIDTH = 256;
	private static final int TEXTURE_HEIGHT = 256;
	private static final int POSITION_MAN_WIDTH = 400;
	private static final int POSITION_MAN_HEIGHT = 160;

	// private BitmapTextureAtlas textureBG;
	private BitmapTextureAtlas textureMan;
	private BitmapTextureAtlas textureScore;
	private BitmapTextureAtlas texturePerson;
	// private TextureRegion regionBG;
	private TextureRegion regionMan;
	private TextureRegion regionPerson;
	// private TiledTextureRegion tiledTextureRegion;
	private Font mFont;

	@Override
	public Engine onLoadEngine() {
		final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.PORTRAIT,
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera));
	}

	@Override
	public void onLoadResources() {
		// Path
		FontFactory.setAssetBasePath("font/");
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

		// this.textureBG = new BitmapTextureAtlas(TEXTURE_WIDTH,
		// TEXTURE_HEIGHT, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.textureMan = new BitmapTextureAtlas(TEXTURE_WIDTH, TEXTURE_HEIGHT,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.texturePerson = new BitmapTextureAtlas(TEXTURE_WIDTH,
				TEXTURE_HEIGHT, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.textureScore = new BitmapTextureAtlas(TEXTURE_WIDTH,
				TEXTURE_HEIGHT, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

		// this.regionBG = BitmapTextureAtlasTextureRegionFactory
		// .createFromAsset(this.textureBG, this, "bg.png", 0, 0);
		this.regionMan = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.textureMan, this, "file_copy.png", 0, 0);
		this.regionPerson = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.texturePerson, this, "file_copy.png", 0,
						0);
		this.mFont = FontFactory.createFromAsset(this.textureScore, this,
				"AllHookedUp.ttf", 20, true, Color.YELLOW);
		// this.tiledTextureRegion = BitmapTextureAtlasTextureRegionFactory
		// .createTiledFromAsset(this.textureIMage, this, "BG.jpeg",
		// 0, 0, 2, 1);
		this.enableAccelerometerSensor(this);

		this.mEngine.getTextureManager().loadTextures(this.textureMan,
				this.textureScore);
		this.mEngine.getFontManager().loadFont(this.mFont);
	}

	public Thread th = new Thread();
	Sprite spritePerson;
	Sprite spriteMan;

	@Override
	public Scene onLoadScene() {
		Scene scene = new Scene();
		this.spriteMan = new Sprite(POSITION_MAN_HEIGHT, POSITION_MAN_WIDTH,
				this.regionMan);
		// Sprite spriteBG = new Sprite(512, 512, this.regionBG);
		final ChangeableText cText = new ChangeableText(10, 20, this.mFont, "",
				" XXXXX".length());
		this.spritePerson = new Sprite(20, 20, this.regionPerson);
		// AnimatedSprite aSprite = new AnimatedSprite(10, 10,
		// tiledTextureRegion);
		// aSprite.animate(1000);
		// scene.attachChild(this.spritePerson);
		// scene.attachChild(spriteBG);
		Thread th = new Thread() {
			@Override
			public void run() {
				int detik = 60;
				int menit = 1;
				while (menit > -1) {
					try {
						sleep(200);
						cText.setText(menit + " : " + detik);
						detik--;
						if (menit < 0 ){
							cText.setText("0 : 0");
						} else if (detik < 0) {
							detik = 60;
							menit--;
						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		};
		th.start();
		scene.registerTouchArea(spriteMan);
		scene.setTouchAreaBindingEnabled(true);
		scene.attachChild(this.spriteMan);
		scene.attachChild(cText);
		return scene;
	}

	@Override
	public void onLoadComplete() {

	}

	@Override
	public void onAccelerometerChanged(AccelerometerData pAccelerometerData) {
		this.spriteMan.setPosition(
				this.spriteMan.getX() + pAccelerometerData.getX(),
				POSITION_MAN_WIDTH);
		if (this.spriteMan.getX() < 0) {
			this.spriteMan.setPosition(0, POSITION_MAN_WIDTH);
		} else if (this.spriteMan.getX() > (CAMERA_WIDTH - this.spriteMan
				.getWidth())) {
			this.spriteMan.setPosition(
					CAMERA_WIDTH - this.spriteMan.getWidth(),
					POSITION_MAN_WIDTH);
		}
	}

	@Override
	public boolean onTouchEvent(TouchEvent pTouchEvent) {
		switch (pTouchEvent.getAction()) {
		case TouchEvent.ACTION_DOWN:
			Toast.makeText(getApplicationContext(), "BALLL", Toast.LENGTH_SHORT)
					.show();
			break;
		default:
			break;
		}
		return false;
	}

}
